const http = require('http');
const fs = require('fs');
const path = require('path');
http.createServer((req,res)=>{
    if(req.url == '/'){
        // res.setHeader('Content-Type','text/plain;charset=utf-8');
        // res.end('这是我的首页');
        res.setHeader('Content-type','text/html;charset=utf-8');//文本是plain 页面是html
        fs.readFile(path.join(__dirname,'html','index.html'),(err,data)=>{
            if(err){
                console.log(err);
            }else{
                console.log(data);
                res.end(data); 
            }
        })
    }else if(req.url == '/login'){
        res.setHeader('Content-type','text/plain;charset=utf-8');//请求头 
        res.end('这是登录！！');//响应请求 输出这是登录
    }else if(req.url == '/register'){
        res.setHeader('Content-type','text/plain;charset=utf-8');
        res.end('这是注册');
    }else if(req.url == '/home'){
        res.setHeader('Content-type','text/plain;charset=utf-8');
        res.end('这是home');
    }else if(req.url == '/img/konglong.jpg'){
        fs.readFile(path.join(__dirname,'img','konglong.jpg'),(err,data)=>{
            if(!err){
                console.log(data);
                res.setHeader('Content-type','application/x-jpg;charset=utf-8');
                res.end(data);
            }
        })
    }
}).listen(8080,(err)=>{
    if(!err){
        console.log('服务器启动成功！');
    }else{
        console.log(err);
    }
})